package com.tutor.lzm.function.basic;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.tutor.lzm.R;
import com.tutor.lzm.activity.BaseActivity;
import com.tutor.lzm.activity.LoginActivity;
import com.tutor.lzm.function.teacher.act.AddCharacterAct;
import com.tutor.lzm.utils.UpdateManager;
import com.tutor.syslib.util.FileUtils;
import com.tutor.syslib.util.MessageTools;
import com.tutor.syslib.util.TextUtil;

import java.io.File;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by winning_zzg on 2017/6/1.
 * 设置
 */
public class SettingActivity extends BaseActivity {

    @BindView(R.id.ly_update)
    LinearLayout lyUpdate;
    @BindView(R.id.ly_sign_out)
    LinearLayout lySignOut;


    public static void start(Context context) {
        Intent intent = new Intent(context, SettingActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        initTitle("设置");
    }

//    @OnClick({R.id.ly_update, R.id.ly_sign_out})
//    public void onClick(View view) {
//        switch (view.getId()) {
//            case R.id.ly_update:
//                showMydialog("正在检测中，请稍后...");
//                mHandler.sendEmptyMessageDelayed(1, 2 * 1000);
//                break;
//            case R.id.ly_sign_out:
//                MessageTools.showDialogf(mAct, "是否退出登录", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        appSession.setPassword("");
//                        Intent intent = new Intent(mCtx, LoginActivity.class);
//                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
//                        startActivity(intent);
//                    }
//                });
//
//                break;
//        }
//    }

//    private Handler mHandler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            closeDialog();
//            MessageTools.showDialogOk(SettingActivity.this, "当前已经是最新版本");
//        }
//    };

    @OnClick({R.id.ly_update, R.id.ly_sign_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ly_update:
//                showMyDialog("正在检测中，请稍后...");
//                mHandler.sendEmptyMessageDelayed(1, 2 * 1000);
                UpdateManager.getUpdateManager().checkAppUpdate(SettingActivity.this, true, true);
                break;
            case R.id.ly_sign_out:
                loginOut();
                break;
        }
    }


    private void loginOut() {
        MessageTools.showDialog(oThis, "是否确定退出登录...", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);

            }
        });
    }

    class SetCacheFileTask extends AsyncTask<Void, Void, ArrayList<File>>

    {
        @Override
        protected ArrayList<File> doInBackground(Void... params) {
            ArrayList<File> cacheFile = new ArrayList<File>();
            //获取缓存文件
            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                    || !Environment.isExternalStorageRemovable()) {
                cacheFile.add(FileUtils.getExternalCacheFile(oThis));
                cacheFile.add(getApplicationContext().getCacheDir());
            } else {
                cacheFile.add(getApplicationContext().getCacheDir());
            }
            //得到图片缓存路径  需要做非空判断
//        File file = ImageLoader.getInstance().getDiskCache().getDirectory();

            return cacheFile;
        }

        @Override
        protected void onPostExecute(ArrayList<File> aVoid) {
            super.onPostExecute(aVoid);
//        tv_cache.setText(FileOperationHelper.getCacheFileLength(aVoid));
        }
    }
}
